package com.caffeinatedbliss.nanowrimo.spike;

import com.caffeinatedbliss.nanowrimo.DynamicImageGenerator;

import java.awt.*;
import java.awt.image.BufferedImage;

public class NanowidgetIcon implements DynamicImageGenerator {
  private static final int CURVE_RADIUS = 15;
  public static final int WIDTH = 75;
  public static final int HEIGHT = 75;

  public static final Color LIGHTEST_GREY = new Color(0xf0, 0xf0, 0xf0);
  public static final Color NOT_SO_LIGHTEST_GREY = new Color(0xbb, 0xbb, 0xdd);
  private static final GradientPaint GRADIENT = new GradientPaint(CURVE_RADIUS, CURVE_RADIUS,
    LIGHTEST_GREY, 2 * CURVE_RADIUS, CURVE_RADIUS * 7, NOT_SO_LIGHTEST_GREY, false);

  public static final Color GREY_BORDER = new Color(0xbb, 0xbb, 0xbb);
  public static final Color DK_GREY_BORDER = new Color(0x99, 0x99, 0x99);
  private static final GradientPaint OUTSIDE_GRADIENT = new GradientPaint(CURVE_RADIUS, CURVE_RADIUS,
    GREY_BORDER, 2 * CURVE_RADIUS, CURVE_RADIUS * 7, DK_GREY_BORDER, false);

  public static final Color DK_TEXT_COLOR = new Color(0x33, 0x33, 0x66);
  public static final Color TEXT_COLOR = new Color(0x80, 0x80, 0x99);
  private static final GradientPaint TEXT_GRADIENT = new GradientPaint(CURVE_RADIUS, CURVE_RADIUS,
    TEXT_COLOR, 2 * CURVE_RADIUS, CURVE_RADIUS * 4, DK_TEXT_COLOR, false);

  public static final Color GRAPH_BACKGROUND = new Color(0xee, 0xee, 0xee);
  public static final Color GREEN_INK = new Color(0x00, 0xcc, 0x00);

  private BufferedImage graphImage;

  public NanowidgetIcon() {

    graphImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = (Graphics2D) graphImage.getGraphics();

    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    drawFilledRectangleWithCurvedCorners(g);

    g.setPaint(TEXT_GRADIENT);
    Font f = new Font("dialog", Font.BOLD, 18);
    drawCenteredText(g, f, "NaNo-", 5);
    drawCenteredText(g, f, "WriMo", 25);
    f = new Font("dialog", Font.BOLD, 10);
    drawCenteredText(g, f, "Progress", 45);

    g.dispose();
  }

  private void drawCenteredText(Graphics g, Font textFont, String aTextString, int yPos) {
    g.setFont(textFont);
    FontMetrics fm = g.getFontMetrics(textFont);
    int ascent = fm.getAscent();
    int descent = fm.getDescent();
    int totalLineHeght = ascent + descent;
    int wordWidth = fm.stringWidth(aTextString);
    int leftEdge = (WIDTH - wordWidth) / 2;
    g.drawString(aTextString, leftEdge, yPos + totalLineHeght);
  }

  private void drawFilledRectangleWithCurvedCorners(Graphics2D g) {
    g.setPaint(GRADIENT);
    g.fillRoundRect(0, 0, WIDTH - 1, HEIGHT - 1, 2 * CURVE_RADIUS, 2 * CURVE_RADIUS);

    g.setPaint(OUTSIDE_GRADIENT);
    g.drawRoundRect(0, 0, WIDTH - 1, HEIGHT - 1, 2 * CURVE_RADIUS, 2 * CURVE_RADIUS);
  }

  public BufferedImage getGraphImage() {
    return graphImage;
  }
}
